// pages/discover/news/news-list.js
const app = getApp()
import utils from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    serviceList: null,
    noMore: false,
    kw: '',
    noSearch: false,
    totalrecords: null,
    page: null,
    userInfo: {},
    notLogin: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
  },

  onShow(){
    let isLogin = utils.checkLogin()
    if(!isLogin){
      //进入授权登录页面
      wx.redirectTo({
        url: '/pages/user/login/login',
      })
    }
    if(wx.getStorageSync('LOGIN_USER')!=''){
      this.initUserInfo()
    }
    this.setData({serviceList: null, noMore: false})
  },

  //加载当前登录用户的信息
  initUserInfo: function(){
    let that = this
    //到服务端实时load一下用户信息
    utils.request(
      app.globalData.apiBaseUrl+'/load/user',
      {},
      'POST'
    ).then(res=>{
      console.log(res)
      that.setData({userInfo: res.data, notLogin: false})
      wx.setStorageSync("LOGIN_USER", res.data)
      app.globalData.userInfo = res.data 
      wx.hideLoading()
      that.loadServiceList()
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({serviceList: null})
    this.loadServiceList()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.serviceList==null || this.data.serviceList.currentpage==this.data.serviceList.totalpage){
      this.setData({noMore:true})
      return
    }
    let tmp = this.data.serviceList
    tmp.currentpage=tmp.currentpage+1
    this.setData({serviceList: tmp})
    this.loadNews()
  },

  //查询列表信息
  loadServiceList: function(){
    let that = this
    let req = {productType: 2, currCity: this.data.currCity, tag: this.data.tag}
    if(this.data.serviceList!=null){
      req.total=this.data.serviceList.totalrecord,
      req.page=this.data.serviceList.currentpage
    }
    let url = app.globalData.apiBaseUrl+'/my/service/list'
    if(this.data.userInfo.role==2){
      url = app.globalData.apiBaseUrl+'/service/list'
    }
    utils.request(
      url,
      req
    ).then(res=>{
      wx.stopPullDownRefresh()
      // console.log('查询结果：', res)
      let existList = that.data.serviceList
      if(existList!=null){
        existList.records=existList.records.concat(res.data.records)
        that.setData({
          serviceList: existList
        })
      }else{
        that.setData({
          serviceList: res.data,
          totalrecords: res.data.totalrecords,
          page: res.data.page
        })
      }
      let noMore = false
      if(res.data.page==res.data.totalpage||res.data.records.length==0){
        noMore=true 
      }
      that.setData({noMore: noMore})
    })
  },

  bindKwInput: function(e){
    this.setData({ kw: e.detail.value })
  },

  toDetailPage: function(e){
    let bid = e.currentTarget.dataset.pid
    console.log('跳转到详情页',bid)
    wx.navigateTo({
      url: '/pages/service/service-detail?pid='+bid,
    })
  },

  toAddPage: function(e){
    wx.navigateTo({
      url: '/pages/service/publish',
    })
  },

  onClearSearch: function(e){
    console.log('EEEE')
    this.setData({kw: ''})
  },

  onHide(){
    this.setData({kw: ''})
  },

  //筛选类型
  bindCategoryFilter: function(e){
    this.setData({serviceList: null})
    let category = e.currentTarget.dataset.tag
    console.log(category)
    this.setData({tag: category})
    this.loadServiceList()
  },

  onCall: function(e){
    let mobile = this.data.userInfo.community.tel
    wx.makePhoneCall({
      phoneNumber: mobile
    })
  },
})